<!-- 公共文件 start -->
@extends('admin.public.base')
<!-- 公共文件 end -->
<style>
    table th {
        min-width: 10px !important;
    }
    .layui-card-header {
        padding-top: 10px !important;
    }
</style>
@section('content')
    <div class="x-nav">
    <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">商品管理</a>
        <a><cite>组合商品</cite></a>
    </span>
        <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
           onclick="location.reload()" title="刷新">
            <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
        </a>
    </div>
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body ">
                        <form class="layui-form layui-col-space5">
                            <div class="layui-inline layui-show-xs-block">
                                <input class="layui-input" autocomplete="off" placeholder="请输入商品名称" name="goods_name" value="{{request()->input('goods_name')}}">
                            </div>
                            <div class="layui-inline layui-show-xs-block">
                                <button class="layui-btn" lay-submit="" lay-filter="sreach">
                                    <i class="layui-icon">&#xe615;搜索</i>
                                </button>
                            </div>
                        </form>
                    </div>
                    <div class="layui-card-header">
                        <button class="layui-btn" onclick="xadmin.open('添加组合商品','/admin/goods/goodsAdd?goods_model=2',1200,630)"><i class="layui-icon"></i>添加</button>
                    </div>
                    <div class="layui-card-body ">
                        <table class="layui-table layui-form" style="width: 100%">
                            <thead>
                            <tr>
                                <th>分类</th>
                                <th>编号</th>
                                <th>图片</th>
                                <th>名称</th>
                                <th>价格</th>
                                <th>起订量</th>
                                <th>促销</th>
                                <th>固定价格</th>
                                <th>状态</th>
                                <th>更新时间</th>
                                <th>操作</th>
                            </thead>
                            <tbody>
                            @foreach($data as $item)
                                <tr>
                                    <td>
                                        {{$item['cate_name']}}
                                    </td>
                                    <td>
                                        {{$item['goods_sn']}}
                                    </td>
                                    <td>
                                        <img src="{{$item['goods_img']}}" style="width: 100px;height: 100px">
                                    </td>
                                    <td>
                                        {{$item['goods_name']}}
                                    </td>
                                    <td>
                                        {{$item['goods_price']}}
                                    </td>
                                    <td>
                                        {{$item['mini_quantity']}}
                                    </td>
                                    <td>
                                        @if($item['promotion'] == 1)
                                            <span style="color: #009688">是</span>
                                        @else
                                            <span style="color: #FF5722">否</span>
                                        @endif
                                    </td>
                                    <td>
                                        @if($item['fixed_price'] == 1)
                                            <span style="color: #009688">是</span>
                                        @else
                                            <span style="color: #FF5722">否</span>
                                        @endif
                                    </td>
                                    <td>
                                        @if($item['status'] == 1)
                                            <span style="color: #009688">已上架</span>
                                        @else
                                            <span style="color: #FF5722">已下架</span>
                                        @endif
                                    </td>
                                    <td>
                                        {{$item['updated_at']}}
                                    </td>
                                    <td class="td-manage" style="min-width: 140px">
                                        <button class="layui-btn layui-btn layui-btn-xs"  onclick="xadmin.open('编辑组合商品','/admin/goods/goodsEdit?id={{$item['id']}}',1200,630)" ><i class="layui-icon">&#xe642;</i>编辑</button>
                                        <button class="layui-btn-danger layui-btn layui-btn-xs"  onclick="goods_del(this,{{$item['id']}})" href="javascript:;" ><i class="layui-icon">&#xe640;</i>删除</button>
                                    </td>
                                </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>
                    <div class="layui-card-body ">
                        <div class="page">
                            {{ $data->links('admin/public/page') }}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        layui.use(['form'], function(){
            form = layui.form;

        });

        // 删除
        function goods_del(obj,id){
            layer.confirm('确认要删除吗？',function(index){
                //发异步删除数据
                $.ajax({
                    type: 'POST',
                    url: '/admin/goods/goodsDel',
                    data: {
                        id: id
                    },
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    },
                    success: function (data) {
                        if (data.code == 1) {
                            $(obj).parents("tr").remove();
                            layer.msg('已删除',{icon:1,time:2000});
                        } else {
                            layer.alert(data.msg, {icon: 7, time: 2000, closeBtn: false, btn: []});
                        }
                    },
                    error: function (xhr, type) {
                        alert('Ajax error!')
                    }
                });
            });
        }

        // 分类展开收起的分类的逻辑
        $(function(){
            $("tbody.x-cate tr[fid!='0']").hide();
            // 栏目多级显示效果
            $('.x-show').click(function () {
                if($(this).attr('status')=='true'){
                    $(this).html('&#xe625;');
                    $(this).attr('status','false');
                    cateId = $(this).parents('tr').attr('cate-id');
                    $("tbody tr[fid="+cateId+"]").show();
                }else{
                    cateIds = [];
                    $(this).html('&#xe623;');
                    $(this).attr('status','true');
                    cateId = $(this).parents('tr').attr('cate-id');
                    getCateId(cateId);
                    for (var i in cateIds) {
                        $("tbody tr[cate-id="+cateIds[i]+"]").hide().find('.x-show').html('&#xe623;').attr('status','true');
                    }
                }
            })
        })

        var cateIds = [];
        function getCateId(cateId) {
            $("tbody tr[fid="+cateId+"]").each(function(index, el) {
                id = $(el).attr('cate-id');
                cateIds.push(id);
                getCateId(id);
            });
        }
    </script>
@endsection
